package com.ji;

import com.ji.protocol.JCMsgHeader;
import com.ji.protocol.JCProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

/**
 * Title:
 * Description:
 * Author:吉
 * Since:2017/5/19
 * Version:1.1.0
 */
public class NioContext {

    private ChannelHandlerContext context;

    public NioContext(ChannelHandlerContext context) {
        this.context = context;
    }

    public void close() {
        context.close();
    }

    public void write(String msg) {
        JCProtocol jcProtocol=new JCProtocol(new JCMsgHeader(msg.getBytes().length),msg.getBytes());
        context.writeAndFlush(jcProtocol).addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                System.out.println(future.isSuccess());
                if(!future.isSuccess()){
                    future.cause().printStackTrace();
                }
            }
        });
    }
}
